package com.xuerhua.yxt.service.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.xuerhua.yxt.common.Const;
import com.xuerhua.yxt.dao.OrganizeGroupMapper;
import com.xuerhua.yxt.dao.TeacherInfoMapper;
import com.xuerhua.yxt.model.OrganizeGroup;
import com.xuerhua.yxt.model.TeacherInfo;
import com.xuerhua.yxt.model.TeacherOrganizeGroup;
import com.xuerhua.yxt.page.Page;
import com.xuerhua.yxt.service.IOrganizeGroupService;
import com.xuerhua.yxt.utils.StringUtil;
import com.xuerhua.yxt.view.TermView;
import com.xuerhua.yxt.wechat.utils.WechatGlobalMap;

@Service("organizeGroupService")
public class OrganizeGroupServiceImpl implements IOrganizeGroupService {

	@Autowired
	private OrganizeGroupMapper organizeGroupMapper;
	@Autowired
	private TeacherInfoMapper teacherInfoMapper;
	
	@Override
	public int insertSelective(OrganizeGroup record) {
		return organizeGroupMapper.insertSelective(record);
	}

	@Override
	public OrganizeGroup selectByPrimaryKey(Integer id) {
		return organizeGroupMapper.selectByPrimaryKey(id);
	}

	@Override
	public int updateByPrimaryKeySelective(OrganizeGroup record) {
		return organizeGroupMapper.updateByPrimaryKeySelective(record);
	}

	@Override
	public List<OrganizeGroup> selectListByPage(Page<OrganizeGroup> page) {
		return organizeGroupMapper.selectListByPage(page);
	}

	@Override
	public int isLeaderGroup(Integer teacherId) {
		return organizeGroupMapper.isLeaderGroup(teacherId);
	}

	@Override
	public int isCourseApprovalGroup(Integer teacherId) {
		return organizeGroupMapper.isCourseApprovalGroup(teacherId);
	}

	@Override
	public List<TeacherOrganizeGroup> fillTeacherOrganizeGroup(OrganizeGroup og) {
		List<OrganizeGroup> list = organizeGroupMapper.selectListByParam(og);
		return getOrganizeGroup(list,og.getSchoolId());
	}

	@Override
	public List<TeacherOrganizeGroup> fillTeacherOrganizeGroupByGroupIds(OrganizeGroup og) {
		List<String> groupIdList = Arrays.asList(og.getIds().split(","));
		List<OrganizeGroup> list = organizeGroupMapper.selectListByGroupIds(og.getSchoolId(), groupIdList);
		return getOrganizeGroup(list,og.getSchoolId());
	}
	
	private List<TeacherOrganizeGroup> getOrganizeGroup(List<OrganizeGroup> list, Integer schoolId) {
		List<TeacherOrganizeGroup> departList = new ArrayList<>();
		if(list != null && list.size() > 0) {
			for(OrganizeGroup menu : list) {
				String key = null;
				Integer parentId = menu.getParentId();
				if(!(parentId != null && parentId != 0)) {
					key = "root";
				}else {
					key = String.valueOf(parentId);
				}
				TeacherOrganizeGroup teacherOrganize = new TeacherOrganizeGroup();
				if(key.equals("root")) {
					teacherOrganize.setGroupId(menu.getId());
					teacherOrganize.setGroupName(menu.getGroupName());
					teacherOrganize.setParentGroupId(menu.getParentId());
					teacherOrganize.setIds(menu.getIds());
					departList.add(teacherOrganize);
				}else {
					teacherOrganize.setGroupId(menu.getId());
					teacherOrganize.setGroupName(menu.getGroupName());
					teacherOrganize.setParentGroupId(menu.getParentId());
					teacherOrganize.setIds(menu.getIds());
					teacherOrganize.setGroupCode(menu.getGroupCode());
					for(TeacherOrganizeGroup parent : departList) {
						if(parentId.intValue() == parent.getGroupId().intValue()) {
							parent.setHasChild(true);
							parent.getSubList().add(teacherOrganize);
						}
					}
				}
			}

			List<TeacherInfo> teacherList = teacherInfoMapper.selectAll(schoolId);
			if(teacherList != null && teacherList.size() > 0) {
				TermView view = (TermView) WechatGlobalMap.get(Const.CACHE_TERM).getValue();
				for(TeacherInfo teacher : teacherList) {
					for(TeacherOrganizeGroup group : departList) {
						if(StringUtil.isExistPosition(group.getIds(), teacher.getPosition())) {
							if(!group.isHasChild()) {
								TeacherOrganizeGroup teacherOrganize = new TeacherOrganizeGroup();
								teacherOrganize.setTeacherId(teacher.getId());
								teacherOrganize.setTeacherName(teacher.getName());
								teacherOrganize.setGroupId(group.getGroupId());
								teacherOrganize.setGroupName(group.getGroupName());
								teacherOrganize.setParentGroupId(group.getParentGroupId());
								group.getSubList().add(teacherOrganize);
							}else {
								for(TeacherOrganizeGroup secondGroup : group.getSubList()) {
									TeacherOrganizeGroup teacherOrganize = new TeacherOrganizeGroup();
									teacherOrganize.setTeacherId(teacher.getId());
									teacherOrganize.setTeacherName(teacher.getName());
									if(secondGroup.getGroupCode().equals("SUBJECT_GROUP") && 
											StringUtil.isExistPosition(secondGroup.getIds(), String.valueOf(teacher.getSubjectId()))) {
										teacherOrganize.setGroupId(secondGroup.getGroupId());
										teacherOrganize.setGroupName(secondGroup.getGroupName());
										teacherOrganize.setParentGroupId(secondGroup.getParentGroupId());
										secondGroup.getSubList().add(teacherOrganize);
									}else if(secondGroup.getGroupCode().equals("GRADE_GROUP")) {
										int ids = (view.getTermKey() - teacher.getGrade()) + 1;
										if(ids == Integer.valueOf(secondGroup.getIds()).intValue()) {
											teacherOrganize.setGroupId(secondGroup.getGroupId());
											teacherOrganize.setGroupName(secondGroup.getGroupName());
											teacherOrganize.setParentGroupId(secondGroup.getParentGroupId());
											secondGroup.getSubList().add(teacherOrganize);
										}
									}
								}
							}
						}
					}
				}
			}
		}
		return departList;
	}

}
